package fr.umlv.m2.jee.pmlv.service.planning.impl;

import fr.umlv.m2.jee.pmlv.persistence.planning.AbstractEvent;
import fr.umlv.m2.jee.pmlv.persistence.planning.Projector;
import fr.umlv.m2.jee.pmlv.persistence.planning.dao.HibernateAbstractEventDAO;
import fr.umlv.m2.jee.pmlv.persistence.planning.dao.HibernateProjectorDAO;
import fr.umlv.m2.jee.pmlv.service.planning.DisplayAbstractEvent;
import fr.umlv.m2.jee.pmlv.service.planning.IDisplayAbstractEventService;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Stephanie
 */
@Service("defautDisplayAbstractEventService")
public class DefautDisplayAbstractEventService implements IDisplayAbstractEventService {

    @Autowired
    @Qualifier("hibernateAbstractEventDAO")
    private HibernateAbstractEventDAO dao;
    @Autowired
    @Qualifier("hibernateProjectorDAO")
    private HibernateProjectorDAO pdao;

    @Override
    @Transactional(readOnly=true)
    public List<DisplayAbstractEvent> getAllDisplayAbstractEvent() {
        ArrayList< DisplayAbstractEvent > result = new ArrayList< DisplayAbstractEvent >(16);
        for(AbstractEvent event : dao.findAll() ) {
            result.add(new DisplayAbstractEvent(event));
        }
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public List<DisplayAbstractEvent> getAbstractEventByWeekAndByProfessorUidAndByYear(GregorianCalendar start, String uid, int year) {
        ArrayList< DisplayAbstractEvent > result = new ArrayList< DisplayAbstractEvent >(16);
        for(AbstractEvent event : dao.findByWeekAndByProfessorUidAndByYear(start, uid, year) ) {
            result.add(new DisplayAbstractEvent(event));
        }
        Collections.sort(result, new Comparator<DisplayAbstractEvent>() {
            @Override
            public int compare(DisplayAbstractEvent o1, DisplayAbstractEvent o2) {
                return o1.getBegin().compareTo(o2.getBegin());
            }
        });
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public List<DisplayAbstractEvent> getAbstractEventByWeekAndByStudentUidAndByYear(GregorianCalendar start, String uid, int year) {
        ArrayList< DisplayAbstractEvent > result = new ArrayList< DisplayAbstractEvent >(16);
        for(AbstractEvent event : dao.findByWeekAndByStudentUidAndByYear(start, uid, year) ) {
            result.add(new DisplayAbstractEvent(event));
        }
        Collections.sort(result, new Comparator<DisplayAbstractEvent>() {
            @Override
            public int compare(DisplayAbstractEvent o1, DisplayAbstractEvent o2) {
                return o1.getBegin().compareTo(o2.getBegin());
            }
        });
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public List<DisplayAbstractEvent> getAbstractEventByWeekAndBySecretaryUidAndByYear(GregorianCalendar start, String uid, int year) {
        ArrayList< DisplayAbstractEvent > result = new ArrayList< DisplayAbstractEvent >(16);
        for(AbstractEvent event : dao.findByWeekAndBySecretaryUidAndByYear(start, uid, year) ) {
            result.add(new DisplayAbstractEvent(event));
        }
        Collections.sort(result, new Comparator<DisplayAbstractEvent>() {
            @Override
            public int compare(DisplayAbstractEvent o1, DisplayAbstractEvent o2) {
                return o1.getBegin().compareTo(o2.getBegin());
            }
        });
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public void reserveProjector(long event_id) {
    	AbstractEvent abstractEvent = dao.findById(event_id);
    	abstractEvent.setProjector(pdao.findById(Long.parseLong("1")));
        dao.merge(abstractEvent);
    }

    @Override
    @Transactional(readOnly=true)
    public void cancelProjector(long event_id) {
    	AbstractEvent abstractEvent = dao.findById(event_id);
    	abstractEvent.setProjector(null);
        dao.merge(abstractEvent);
    }
}
